package com.example.identifyvisio.service;


import com.example.identifyvisio.model.component.CoordinatesAndParent;
import com.example.identifyvisio.model.component.Position;
import com.example.identifyvisio.model.component.Size;
import com.example.identifyvisio.model.node.Ports;
import com.example.identifyvisio.model.node.startingnode.*;
import com.example.identifyvisio.model.node.startingnode.failnode.FailData;
import com.example.identifyvisio.model.node.startingnode.failnode.FailNode;
import com.example.identifyvisio.model.node.startingnode.startnode.*;
import com.example.identifyvisio.model.node.startingnode.succeednode.SucceedData;
import com.example.identifyvisio.model.node.startingnode.succeednode.SucceedNode;


import java.util.*;
import static com.example.identifyvisio.utis.NodeValueUtil.getObtainCoordinates;
import static com.example.identifyvisio.utis.NodeValueUtil.getPorts;


public class NodeService {

    /**
     * 失败节点数据
     * @param dataMap
     * @param shapeID
     * @param shapeMap
     * @return
     */
    public static FailNode getFail(HashMap<Integer, Map<String, String>> dataMap,
                                   Integer shapeID, Map<String, String> shapeMap,
                                   HashMap<String, List<Position>> positionMap) {
        FailNode node = new FailNode();

        String nodeText = shapeMap.get("text");

        FailData failData = new FailData();
        failData.setType("Fail");
        failData.setLabel(nodeText);
        failData.setStateId(dataMap.get(shapeID).get("stateId"));
        failData.setStateType("Fail");
        HashMap<String, String> failMap = new HashMap<>();
        failMap.put("ErrorCode", "");
        failMap.put("Message", "");
        failData.setFailProps(failMap);
        node.setData(failData);
        //获取坐标和父节点
        CoordinatesAndParent obtainCoordinates = getObtainCoordinates(dataMap, shapeMap,positionMap);

        node.setPosition(obtainCoordinates.getPosition());
        node.setParent(obtainCoordinates.getParent());
        Size obtainSize = obtainCoordinates.getSize();
        Size size = new Size("起始");
        if (obtainSize.getWidth() != 0.0){
            size.setWidth(obtainSize.getWidth());
        }
        if (obtainSize.getHeight() != 0.0){
            size.setHeight(obtainSize.getHeight());
        }
        node.setSize(size);

        Attrs attrs = new Attrs();
        Text text = new Text(nodeText,"#fff",8);
        Body body = new Body("#ea5151","#ea5151",10,10);
        attrs.setBody(body);
        attrs.setText(text);
        node.setAttrs(attrs);

        node.setShape("custom-rect");

        String uniqueID = shapeMap.get("UniqueID");
        node.setId(uniqueID);

        Ports ports = getPorts();

        node.setPorts(ports);

        node.setZIndex("10");

        return node;
    }

    /**
     * 开始节点数据
     * @param dataMap
     * @param shapeID
     * @param shapeMap
     * @return
     */
    public static StartNode getStart(HashMap<Integer, Map<String, String>> dataMap, Integer shapeID,
                                     Map<String, String> shapeMap,HashMap<String, List<Position>> positionMap) {
        StartNode node = new StartNode();

        //获取坐标和父节点
        CoordinatesAndParent obtainCoordinates = getObtainCoordinates(dataMap, shapeMap, positionMap);

        node.setPosition(obtainCoordinates.getPosition());
        node.setParent(obtainCoordinates.getParent());

        Size obtainSize = obtainCoordinates.getSize();
        Size size = new Size("起始");
        if (obtainSize.getWidth() != 0.0){
            size.setWidth(obtainSize.getWidth());
        }
        if (obtainSize.getHeight() != 0.0){
            size.setHeight(obtainSize.getHeight());
        }
        node.setSize(size);

        String nodeText = shapeMap.get("text");

        Attrs attrs = new Attrs();
        Text text = new Text(nodeText,"#fff",8);
        Body body = new Body("#ff9880","#ff9880",10,10);
        attrs.setBody(body);
        attrs.setText(text);
        node.setAttrs(attrs);

        node.setShape("custom-rect");

        String uniqueID = shapeMap.get("UniqueID");
        node.setId(uniqueID);

        StartData startData = new StartData();
        startData.setType("node");
        startData.setLabel(nodeText);

        startData.setStateId(dataMap.get(shapeID).get("stateId"));
        startData.setStateType("Start");
        startData.setStateProps(
                new StateProps(
                        new StateMachine("测试", "Test-001-1.0.0", "1.0.0")
                ));
        node.setData(startData);

        Ports ports = getPorts();

        node.setPorts(ports);

        node.setZIndex("10");
        return node;
    }

    /**
     * 成功节点
     * @param dataMap
     * @param shapeID
     * @param shapeMap
     * @return
     */
    public static SucceedNode getSucceed(HashMap<Integer, Map<String, String>> dataMap, Integer shapeID,
                                         Map<String, String> shapeMap,HashMap<String, List<Position>> positionMap) {


        SucceedNode node = new SucceedNode();

        //获取坐标和父节点
        CoordinatesAndParent obtainCoordinates = getObtainCoordinates(dataMap, shapeMap, positionMap);

        node.setPosition(obtainCoordinates.getPosition());
        node.setParent(obtainCoordinates.getParent());

        Size obtainSize = obtainCoordinates.getSize();
        Size size = new Size("起始");
        if (obtainSize.getWidth() != 0.0){
            size.setWidth(obtainSize.getWidth());
        }
        if (obtainSize.getHeight() != 0.0){
            size.setHeight(obtainSize.getHeight());
        }
        node.setSize(size);

        String nodeText = shapeMap.get("text");

        Attrs attrs = new Attrs();
        Text text = new Text(nodeText,"#fff",8);
        Body body = new Body("#ff9880","#ff9880",10,10);
        attrs.setBody(body);
        attrs.setText(text);
        node.setAttrs(attrs);


        node.setShape("custom-rect");

        String uniqueID = shapeMap.get("UniqueID");
        node.setId(uniqueID);


        Ports ports = getPorts();

        node.setPorts(ports);

        node.setZIndex("10");

        SucceedData succeedData = new SucceedData();
        succeedData.setType("Succeed");
        succeedData.setLabel(shapeMap.get("text"));
        succeedData.setStateId(dataMap.get(shapeID).get("stateId"));
        succeedData.setStateType("Succeed");
        node.setData(succeedData);

        return node;
    }

    /**
     * 连接线空开头默认连成功节点
     * @param dataMap
     * @param shapeMap
     * @return
     */
    public static String getStart(HashMap<Integer, Map<String, String>> dataMap,
                                     Map<String, String> shapeMap,double pinX ,double pinY,
                                     ArrayList<Object> shapes,HashMap<String, List<Position>> positionMap) {
        StartNode node = new StartNode();

        HashMap<String, String> hashMap = new HashMap<>(shapeMap);
        hashMap.put("PinX", String.valueOf(pinX));
        hashMap.put("PinY", String.valueOf(pinY));
        //获取坐标和父节点
        CoordinatesAndParent obtainCoordinates = getObtainCoordinates(dataMap, hashMap, positionMap);

        node.setPosition(obtainCoordinates.getPosition());
        node.setParent(obtainCoordinates.getParent());

        Size size = new Size("起始");
        node.setSize(size);

        String nodeText = "开始";

        Attrs attrs = new Attrs();
        Text text = new Text(nodeText,"#fff",8);
        Body body = new Body("#ff9880","#ff9880",10,10);
        attrs.setBody(body);
        attrs.setText(text);
        node.setAttrs(attrs);

        node.setShape("custom-rect");

        // 生成随机的UUID
        UUID uuid = UUID.randomUUID();
        // 将UUID转换成字符串形式
        String uniqueID = uuid.toString();
        node.setId(uniqueID);

        StartData startData = new StartData();
        startData.setType("node");
        startData.setLabel(nodeText);

        startData.setStateId("");

        startData.setStateType("Start");
        startData.setStateProps(
                new StateProps(
                        new StateMachine("测试", "Test-001-1.0.0", "1.0.0")
                ));
        node.setData(startData);
        Ports ports = getPorts();
        node.setPorts(ports);

        node.setZIndex("10");
        shapes.add(node);
        return uniqueID;
    }

    /**
     * 连接线空结尾默认连成功节点
     * @param dataMap
     * @param shapeMap
     * @return
     */
    public static String getSucceed(HashMap<Integer, Map<String, String>> dataMap,
                                    Map<String, String> shapeMap,double pinX ,double pinY,
                                    ArrayList<Object> shapes,HashMap<String, List<Position>> positionMap) {

        SucceedNode node = new SucceedNode();

        HashMap<String, String> hashMap = new HashMap<>(shapeMap);
        hashMap.put("PinX", String.valueOf(pinX));
        hashMap.put("PinY", String.valueOf(pinY));
        //获取坐标和父节点
        CoordinatesAndParent obtainCoordinates = getObtainCoordinates(dataMap, hashMap, positionMap);

        node.setPosition(obtainCoordinates.getPosition());
        node.setParent(obtainCoordinates.getParent());

        Size size = new Size("起始");

        node.setSize(size);

        String nodeText = "成功";

        Attrs attrs = new Attrs();
        Text text = new Text(nodeText,"#fff",8);
        Body body = new Body("#ff9880","#ff9880",10,10);
        attrs.setBody(body);
        attrs.setText(text);
        node.setAttrs(attrs);
        node.setShape("custom-rect");
        // 生成随机的UUID
        UUID uuid = UUID.randomUUID();
        // 将UUID转换成字符串形式
        String uniqueID = uuid.toString();
        node.setId(uniqueID);

        Ports ports = getPorts();
        node.setPorts(ports);
        node.setZIndex("10");
        SucceedData succeedData = new SucceedData();
        succeedData.setType("Succeed");
        succeedData.setLabel(nodeText);

        succeedData.setStateId("");
        succeedData.setStateType("Succeed");
        node.setData(succeedData);

//        LogicalProcessing processing = new LogicalProcessing();
//        List<Object> shapes = processing.getShapes();
        shapes.add(node);
//        processing.setShapes(shapes);

        return uniqueID;
    }

}
